<template>
  <div class="view-fill page-asset-asset-application">
    <div
      v-if="
        !showModify.visible &&
        !purchDetailInfo.visible &&
        !contractDetailInfo.visible
      "
      class="app-container-flex"
    >
      <NewFullScreen
        v-model="showDialog"
        :title="title"
        :btn-list="fullInfo.btnList"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button
            v-if="
              $permission('assetApplication:submit') &&
              basicInfoList.data.status === 1
            "
            @click="submit"
            >提交</el-button
          >
          <el-button
            v-if="
              $permission('assetApplication:update') &&
              basicInfoList.data.status === 1
            "
            @click="update"
            >修改</el-button
          >
          <!-- <el-button
            v-if="
              $permission('assetApplication:delete') &&
              basicInfoList.data.status === 1
            "
            @click="remove"
            >删除</el-button
          > -->
          <el-button
            v-if="
              $permission('assetApplication:revoke') &&
              basicInfoList.data.status === 2
            "
            @click="revoke"
            >撤回</el-button
          >
          <el-button
            v-if="
              $permission('assetApplication:reject') &&
              basicInfoList.data.status === 2
            "
            @click="reject"
            >驳回</el-button
          >
          <el-button
            v-if="
              $permission('assetApplication:check') &&
              basicInfoList.data.status === 2
            "
            @click="check"
            >审核</el-button
          >
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :canPrint="
              $permission('assetApplication:printDetail') &&
              basicInfoList.data.status !== 1 &&
              basicInfoList.data.status !== 2
            "
            :templateArr="templateArr"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
          <!-- <el-button
            v-if="
              $permission('assetApplication:printDetail') &&
              basicInfoList.data.status !== 1 &&
              basicInfoList.data.status !== 2
            "
            @click="print"
            >打印</el-button
          > -->
          <el-button
            v-if="
              $permission('assetApplication:excelDetail') &&
              basicInfoList.data.status !== 1 &&
              basicInfoList.data.status !== 2
            "
            @click="exportExcel"
            >导出excel</el-button
          >
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="basicInfoList">
              <template v-slot:basic-purchNo>
                <span
                  v-if="basicInfoList.data.purchNo"
                  class="slip-edit"
                  @click="showPurchDetail"
                >
                  {{ basicInfoList.data.purchNo }}
                </span>
                <span v-else>-</span>
              </template>
              <template v-slot:basic-contractNo>
                <span
                  v-if="basicInfoList.data.contractNo"
                  class="slip-edit"
                  @click="showContractDetail"
                >
                  {{ basicInfoList.data.contractNo }}
                </span>
                <span v-else>-</span>
              </template>
            </basic-information>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('main_use_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="mainUseInfoList" :labelWidth="'169px'">
            </basic-information>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('use_plan_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="usePlanInfoList" :labelWidth="'181px'">
            </basic-information>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('technical_parameter_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information
              :info="technicalParameterInfoList"
              :labelWidth="'210px'"
            >
            </basic-information>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('match_need_condition_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information
              :info="matchNeedConditionInfoList"
              :labelWidth="'195px'"
            >
            </basic-information>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('benefit_analysis_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information
              :info="benefitAnalysisInfoList"
              :labelWidth="'200px'"
            >
            </basic-information>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <asset-application-modify
      v-if="showModify.visible"
      :id="id"
      v-model:visible="showModify.visible"
      :title="showModify.title"
      :type="showModify.type"
      @returnModify="modifyCallback"
    >
    </asset-application-modify>
    <asset-purch-order-detail
      v-if="purchDetailInfo.visible"
      v-model:visible="purchDetailInfo.visible"
      :order-id="purchDetailInfo.orderId"
    />
    <contract-detail
      v-if="contractDetailInfo.visible"
      :id="contractDetailInfo.id"
      v-model:visible="contractDetailInfo.visible"
    />
  </div>
</template>

<script setup name="assetApplicationDetails">
import {
  assetApplicationCheck,
  assetApplicationRevoke,
  assetApplicationReject,
  assetApplicationDelete,
  assetApplicationSubmit,
  selectAssetApplicationById,
  printAssetApplicationDetail,
  exportAssetApplicationDetail,
} from '@/api/asset.js'
import { orderOpAffairAll } from '@/api/affair'
import { fatchTemplateByCode } from '@/api/base'
import { getCustomizeSetting } from '@/api/customize'
import AssetApplicationModify from './modify'
import AssetPurchOrderDetail from '../assetPurch/detail'
import ContractDetail from '@/views/contractManage/contract/details'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'
import { updatePrintCount } from '@/api/mapping'

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps(['visible', 'id'])

const title = ref('资产申购论证详情')
const fullInfo = ref({
  btnList: [{ type: 'slot', value: 'button' }],
})
const basicInfoList = ref({
  data: {},
  fieldList: [
    { label: '单号', value: 'orderNo' },
    { label: '资产字典名称', value: 'assetName' },
    { label: '资产字典编码', value: 'assetCode' },
    { label: '医疗器械分类目录', value: 'deviceTypeValue' },
    { label: '规格', value: 'standard' },
    { label: '品牌', value: 'trademark' },
    { label: '申请部门', value: 'deptName' },
    { label: '部门负责人', value: 'deptManagerName' },
    { label: '数量', value: 'qty' },
    { label: '单价(预算/元)', value: 'price' },
    { label: '总价', value: 'sumAmount' },
    { label: '合同预签数量', value: 'preContractQty' },
    { label: '合同签订数量', value: 'contractQty' },
    { label: '采购预采数量', value: 'prePurchQty' },
    { label: '采购数量', value: 'purchQty' },
    { label: '单位', value: 'unitName' },
    { label: '状态', value: 'statusValue' },
    { label: '需求程度', value: 'needDegreeName' },
    { label: '需求性质', value: 'needQualityName' },
    { label: '资金来源', value: 'capitalSourceName' },
    { label: '参考厂家', value: 'referenceBrand' },
    { label: '参考型号', value: 'referenceStandard' },
    { label: '参考品牌', value: 'referenceMark' },
    { label: '是否首选进口产品', value: 'isFirstImportName' },
  ],
})
const mainUseInfoList = ref({
  data: {},
  fieldList: [{ label: '资产主要用途', value: 'mainUse', className: 'not-br' }],
})
const usePlanInfoList = ref({
  data: {},
  fieldList: [
    { label: '采购后使用规划', value: 'usePlan', className: 'not-br' },
  ],
})
const technicalParameterInfoList = ref({
  data: {},
  fieldList: [
    {
      label: '具体配置及技术参数',
      value: 'technicalParameter',
      className: 'not-br',
    },
  ],
})
const matchNeedConditionInfoList = ref({
  data: {},
  fieldList: [
    { label: '场所是否符合要求', value: 'isFitPlace' },
    { label: '水电配置要求', value: 'hydropowerDemand' },
    {
      label: '污染排放问题及解决措施',
      value: 'pollutionDischargeMeasures',
    },
    {
      label: '科室人员配备、培训情况',
      value: 'trainSituation',
      labelTip: '能否保证仪器资产正常运行',
    },
    {
      label: '是否需要技术申报',
      value: 'needSkillDeclare',
      labelTip: '如有请注明申报情况',
    },
    {
      label: '是否涉及专项经费使用',
      value: 'specialOutlay',
      labelTip: '如有请注明经费名称',
    },
    { label: '其他特殊要求', value: 'otherDemand' },
  ],
})
const benefitAnalysisInfoList = ref({
  data: {},
  fieldList: [
    { label: '预计使用人次', value: 'frequencyValue' },
    { label: '开展项目及收费清单', value: 'itemChargeList' },
    { label: '所需耗材清单', value: 'needMaterialList' },
    { label: '平均人次耗材成本(元)', value: 'averageMaterialCost' },
    { label: '预计每年维修保养成本(元)', value: 'annualMaintenanceCost' },
    { label: '预计年收入(元)', value: 'annualIncome' },
    { label: '计划启用时间', value: 'planUseTime' },
  ],
})
const affairTableInfo = ref({
  api: orderOpAffairAll,
  query: {
    orderType: 60,
    orderId: props.id,
    sort: 'operate_time desc',
  },
})
const customFilterInfo = ref({
  query: {
    queryMap: {
      id: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.id,
      },
    },
  },
})
const showModify = ref({
  title: '修改',
  type: 'update',
  visible: false,
})
const purchDetailInfo = ref({
  visible: false,
  orderId: '',
})
const contractDetailInfo = ref({
  visible: false,
  id: '',
})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})

onMounted(() => {
  init()
  getCustomize()
})
function showPurchDetail() {
  //跳转采购单详情
  purchDetailInfo.value.visible = true
  purchDetailInfo.value.orderId = basicInfoList.value.data.purchId
}
function showContractDetail() {
  //跳转合同详情
  contractDetailInfo.value.visible = true
  contractDetailInfo.value.id = basicInfoList.value.data.contractId
}
function returnBack() {
  emits('returnBack', false)
}
function init() {
  selectAssetApplicationById(props.id).then((res) => {
    if (res && res.success) {
      basicInfoList.value.data = res.data
      mainUseInfoList.value.data = res.data
      usePlanInfoList.value.data = res.data
      technicalParameterInfoList.value.data = res.data
      matchNeedConditionInfoList.value.data = res.data
      benefitAnalysisInfoList.value.data = res.data
    }
  })
}
function update() {
  showModify.value.visible = true
}
//提交
function submit() {
  assetApplicationSubmit([props.id]).then((resp) => {
    if (resp.success) {
      init()
    }
  })
}
//审核
function check() {
  assetApplicationCheck([props.id]).then((resp) => {
    if (resp.success) {
      init()
    }
  })
}
//撤回
function revoke() {
  assetApplicationRevoke([props.id]).then((resp) => {
    if (resp.success) {
      init()
    }
  })
}
//驳回
function reject() {
  assetApplicationReject([props.id]).then((resp) => {
    if (resp.success) {
      init()
    }
  })
}
//删除
function remove() {
  assetApplicationDelete([props.id]).then((resp) => {
    if (resp.success) {
      init()
    }
  })
}

function exportExcel() {
  exportAssetApplicationDetail(customFilterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
const templateArr = ref([])
const popoverVisible = ref(false)
function print() {
  selectByCodeAndDept({
    tempCode: 'assetApplicationDetail',
    deptId: basicInfoList.value.data.deptId,
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            orderId: props.id,
            orderNo: basicInfoList.value.data.orderNo,
          },
          callback: printCallback,
          printCount: true,
          data: {},
        })
      }
    }
  })
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      orderId: props.id,
      orderNo: basicInfoList.value.data.orderNo,
    },
    callback: printCallback,
    printCount: true,
    data: {},
  })
  popoverVisible.value = false
}
function printCallback() {
  try {
    // 打印成功增加打印次数
    updatePrintCount({
      orderNo: basicInfoList.value.data.orderNo,
      tempCode: 'assetApplicationDetail',
      orderId: basicInfoList.value.data.id,
      orderType: 60,
    })
  } catch (error) {}
}
function getPrintData(temp) {
  let params = {
    customizeDto: customFilterInfo.value.query,
  }
  printAssetApplicationDetail(params).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
function getCustomize() {
  let param = {
    tableKey:
      'assetApplicationList-com.slip.spd.entity.vo.asset.AssetApplicationVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      customFilterInfo.value.query.customizeUserVO = data
    }
  })
}
function modifyCallback() {
  init()
}
</script>
<style scoped lang="scss">
:deep(.box-scroll-y) {
  height: calc(100% - 55px);
}
.page-asset-asset-application {
  :deep(.el-form-item--default .el-form-item__label) {
    width: 200px !important;
  }
}
</style>
